#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
int h[MAXN];
int dp[MAXN];
int main(){
	int n,k;
	cin >> n >> k;
	for(int i = 1;i <= n;i++){
		cin >> h[i];
	}
	//考虑转移状态,dp[i] = min(dp[j] + |hi - hj|)
	memset(dp,0x3f,sizeof(dp));
	dp[1] = 0;
	for(int i = 2;i <= n;i++){
		for(int j = max(1,i - k);j < i;j++){
			dp[i] = min(dp[i],dp[j] + abs(h[i] - h[j]));
		}
	}
	cout << dp[n] << "\n";
}